package com.scalar.chiptrack.operations.finishedGoods.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.finishedGoods.FinishedGoodsHelper;
import com.scalar.chiptrack.operations.finishedGoods.actionCommands.FinishedGoodsActionCommandFactory;
import com.scalar.chiptrack.operations.finishedGoods.forms.FinishedGoodsSearchForm;
import com.scalar.chiptrack.operations.process.dao.ProductDao;

/**
 * User: Murali Krishna Yengala
 * Date: May 26, 2005
 * Time: 12:52:54 PM
 */
public class FinishedGoodsAction extends ChipTrackExtendedAction{

    public ActionForward executeRequest( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        FinishedGoodsSearchForm finishedGoodsSearchForm = (FinishedGoodsSearchForm) form;
        String action = request.getParameter("action");
        ActionForward actionForward = mapping.findForward( "finishedGoodsSearch" );
        boolean addRequestAttribs = true;
        ArrayList fgiProductsList = new ArrayList();


        if( ( action == null ) || ( action.trim().length() ==0 ))
        {
            finishedGoodsSearchForm = new FinishedGoodsSearchForm();
            addRequestAttribs = true;
        }
        else if ( "getFgiParts".equalsIgnoreCase(action) )
        {
             int baseProductIdInt =  finishedGoodsSearchForm.getBaseProductId() ;
            try
            {
                fgiProductsList = ProductDao.loadAllFGProductsByBaseProductId( baseProductIdInt );
            }
            catch( ScalarException e)
            {
                errors.put( "Couldn't fetch Base Products List from Database !", null);
            }
            if( fgiProductsList == null || fgiProductsList.isEmpty())
            {
                info.put( "Couldn't find any Finished Goods for this Base Product ", null);
            }
        }
        else if( action != null )
        {
            try
                {
                    ChipTrackExtendedAction finishedGoodsCommand = FinishedGoodsActionCommandFactory.getActionCommand( action );

                    if( finishedGoodsCommand == null )
                    {
                        errors.put( "Invalid request", null );
                    }
                    else
                    {
                        actionForward = finishedGoodsCommand.executeRequest( mapping, form, request, response );
                        addRequestAttribs = false;
                    }
                }
                catch( Exception e )
                {

                }
        }

        if( addRequestAttribs )
        {
            FinishedGoodsHelper.setBaseProductsAndCustomers( request, errors);
            request.setAttribute("fgiProductsList", fgiProductsList );
        }
        request.setAttribute (SessionKeys.INFO_MESSAGES, info);
        request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);
        return actionForward;
    }

}
